//
//  ViewController.swift
//  粒子动画
//
//  Created by 我演示 on 2019/11/5.
//  Copyright © 2019 ALLSall. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
         
        //1: 创建发射器
        let emitter1  = CAEmitterLayer()
        
        //2: 设置发射器的位置
        emitter1.emitterPosition = CGPoint(x: view.bounds.width * 0.5 , y: view.bounds.height - 120)
        
        //3: 开启三维效果
        emitter1.preservesDepth = true
        
        
        let emitter2  = CAEmitterLayer()
         
         //2: 设置发射器的位置
         emitter2.emitterPosition = CGPoint(x: view.bounds.width * 0.7 , y: view.bounds.height - 120)
         
         //3: 开启三维效果
         emitter2.preservesDepth = true
        //4: 创建粒子，并且创建粒子的相关属性
        let cell = CAEmitterCell()
        //4.1 设置d粒子效果
        
        cell.velocity = 150
        cell.velocityRange = 100 // 范围波动
        
        
        /// 默认情况下，设置粒子的大小
        cell.scale = 0.7
        cell.scaleRange = 0.3
        
        ///4.4 设置粒子的大小f和方向 经度和纬度
        cell.emissionLatitude = CGFloat( -Double.pi / 2)
        cell.emissionRange = CGFloat(Double.pi )
        
        //4,6 设置粒子的弹出效果
        cell.lifetime =  6
        cell.lifetimeRange = 1.5
        
        //4.5 设置粒子的每秒弹出的个数
        cell.birthRate = 10
        
        //4.6 设置粒子展示的图片
        cell.contents = UIImage(named: "tabBar_publish_icon")?.cgImage
        
        //5: 设置粒子到发射qi
        emitter1.emitterCells = [cell]
        emitter2.emitterCells = [cell]
        
        // 6: 讲发射器layer 添加到父layer 中
        
        view.layer.addSublayer(emitter1)
        view.layer.addSublayer(emitter2)
        
    }


}

